package com.beaven.model.model.sp;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import com.beaven.model.Factory;
import com.beaven.model.model.api.account.AccountRspModel;
import com.beaven.model.model.db.User;
import com.beaven.model.model.db.User_Table;
import com.raizlabs.android.dbflow.sql.language.SQLite;

/**
 * author: Beaven
 * time: 2017/5/18 18:41
 */

public class Account {
  private static final String KEY_PUSH_ID = "KEY_PUSH_ID";
  private static final String KEY_IS_BIND = "KEY_IS_BIND";
  private static final String KEY_TOKEN = "KEY_TOKEN";
  private static final String KEY_USER_ID = "KEY_USER_ID";
  private static final String KEY_ACCOUNT = "KEY_ACCOUNT";
  private static final String KEY_NAME = "KEY_NAME";
  private static final String KEY_AVATAR = "KEY_AVATAR";

  // 设备的推送Id
  private static String pushId;
  // 设备Id是否已经绑定到了服务器
  private static boolean isBind;
  // 登录状态的Token，用来接口请求
  private static String token;
  // 登录的用户ID
  private static String userId;
  // 登录的账户
  private static String account;
  // 设备昵称
  private static String name;
  // 头像地址
  private static String avatar;

  // 本地保存
  public static void saveToLocal(Context context) {
    SharedPreferences sp =
        context.getSharedPreferences(Account.class.getSimpleName(), Context.MODE_PRIVATE);
    sp.edit()
        .putString(KEY_PUSH_ID, pushId)
        .putBoolean(KEY_IS_BIND, isBind)
        .putString(KEY_TOKEN, token)
        .putString(KEY_USER_ID, userId)
        .putString(KEY_ACCOUNT, account)
        .putString(KEY_NAME, name)
        .putString(KEY_AVATAR, avatar)
        .apply();
  }

  // 本地加载
  public static void loadFromLocal(Context context) {
    SharedPreferences sp =
        context.getSharedPreferences(Account.class.getSimpleName(), Context.MODE_PRIVATE);
    pushId = sp.getString(KEY_PUSH_ID, "");
    isBind = sp.getBoolean(KEY_IS_BIND, false);
    token = sp.getString(KEY_TOKEN, "");
    userId = sp.getString(KEY_USER_ID, "");
    account = sp.getString(KEY_ACCOUNT, "");
    name = sp.getString(KEY_NAME, "");
    avatar = sp.getString(KEY_AVATAR, "");
  }

  public static boolean clear(Context context) {
    SharedPreferences sp =
        context.getSharedPreferences(Account.class.getSimpleName(), Context.MODE_PRIVATE);
    isBind = false;
    name = account = userId = token = avatar = null;
    return sp.edit().clear().commit();
  }

  /**
   * 设置并存储设备的Id
   */
  public static void setPushId(String pushId) {
    Account.pushId = pushId;
    Account.saveToLocal(Factory.getApplication());
  }

  /**
   * 获取推送Id
   */
  public static String getPushId() {
    return pushId;
  }

  public static void setName(String name) {
    Account.name = name;
    Account.saveToLocal(Factory.getApplication());
  }

  public static String getName() {
    return name;
  }

  /**
   * 返回当前账户是否登录
   */
  public static boolean isLogin() {
    // 用户Id 和 Token 不为空
    return !TextUtils.isEmpty(userId) && !TextUtils.isEmpty(token);
  }

  /**
   * 是否已经完善了用户信息
   */
  public static boolean isComplete() {
    // 首先保证登录成功
    if (isLogin()) {
      User self = getUser();
      return !TextUtils.isEmpty(self.getName());
    }
    // 未登录返回信息不完全
    return false;
  }

  /**
   * 是否已经绑定到了服务器
   *
   * @return True已绑定
   */
  public static boolean isBind() {
    return isBind;
  }

  /**
   * 设置绑定状态
   */
  public static void setBind(boolean isBind) {
    Account.isBind = isBind;
    Account.saveToLocal(Factory.getApplication());
  }

  /**
   * 保存我自己的信息到持久化XML中
   */
  public static void login(AccountRspModel model) {
    // 存储当前登录的账户, token, 用户Id，方便从数据库中查询我的信息
    Account.token = model.getToken();
    Account.account = model.getAccount();
    Account.userId = model.getUser().getId();
    Account.name = model.getUser().getName();
    Account.avatar = model.getUser().getAccount();
    saveToLocal(Factory.getApplication());
  }

  /**
   * 获取当前登录的用户信息
   */
  public static User getUser() {
    // 如果为null返回一个new的User，其次从数据库查询
    return TextUtils.isEmpty(userId) ? new User()
        : SQLite.select().from(User.class).where(User_Table.id.eq(userId)).querySingle();
  }

  /**
   * 返回用户Id
   */
  public static String getUserId() {
    return getUser().getId();
  }

  /**
   * 获取当前登录的Token
   */
  public static String getToken() {
    return token;
  }

  public static String getAvatar() {
    return avatar;
  }
}